package com.mine.distinctionteam;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.mine.distinctionteam.base.BaseActivity;
import com.mine.distinctionteam.ui.home.HomeFragment;
import com.mine.distinctionteam.ui.my.MyFragment;
import com.mine.distinctionteam.ui.sell.MySellFragment;
import com.mine.distinctionteam.ui.shopcar.ShopCarFragment;
import com.mine.distinctionteam.ui.unused.UnUsedFregment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements MainContract.View {
    private MainContract.Presenter mPresenter;
    private Context context=this;
    private FrameLayout layout;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private List<Fragment> list=new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new MainPresenter();
        mPresenter.attachView(this);
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }
    @Override
    public void initViews() {
        list.add(new HomeFragment());
        list.add(new UnUsedFregment());
        list.add(new MySellFragment());
        list.add(new ShopCarFragment());
        list.add(new MyFragment());
        reloadFragment(list,0);
    }
    public void click(View view) {
        switch (view.getId()){
            case R.id.button_home:
                reloadFragment(list,0);
                break;
            case R.id.button_unused:
                reloadFragment(list,1);
                break;
            case R.id.button_sell:
                reloadFragment(list,2);
                break;
            case R.id.button_shopbags:
                reloadFragment(list,3);
                break;
            case R.id.button_myself:
                reloadFragment(list,4);
                break;
        }
    }

    //碎片的转换
    public void reloadFragment(List<Fragment> list, int tabIndex) {
        manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
//        隐藏所有碎片
        for (int i = 0; i < list.size(); i++) {
            transaction.hide(list.get(i));
        }
        Fragment toFragment = list.get(tabIndex);
//        如果加载过显示，没加载过加载
        if (toFragment.isAdded()) {
            transaction.show(toFragment);
        } else {
            transaction.add(R.id.layout_main, toFragment);
            transaction.show(toFragment);
        }
        transaction.commit();
    }

    @Override
    public void initData() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }
}
